<!-- systemSet -->
<template>
  <div class="Leaderboard content_box">
    <h4>分红奖励数据来源</h4>
    <div class="headder_setting float_none m-b-2">
      <el-row class="header_row" :gutter="30">
        <el-col :span="12">
          <div class="flex align-center">
            <span class="tit_span nowarp">销售额：</span>
            <el-input type="number" v-model="Sourcelist.Saleroom" @keyup.native="proving2($event)" min="0">
              <template slot="append">%</template>
            </el-input>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="flex align-center">
            <span class="tit_span nowarp m-r-1">技术服务费(佣金)：</span>
            <el-input type="number" v-model="Sourcelist.ServiceCharge" @keyup.native="proving2($event, '%')" min="0">
              <template slot="append">%</template>
            </el-input>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="flex align-center">
            <span class="tit_span nowarp">技术服务年费：</span>
            <el-input type="number" v-model="Sourcelist.ServiceChargeYear" @keyup.native="proving2($event)" min="0">
              <template slot="append">元/个</template>
            </el-input>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="flex align-center">
            <span class="tit_span nowarp">平台管理费：</span>
            <el-input type="number" v-model="Sourcelist.PlatformFee" @keyup.native="proving2($event)" min="0">
              <template slot="append">%</template>
            </el-input>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="flex align-center">
            <span class="tit_span nowarp">提现手续费：</span>
            <el-input type="number" v-model="Sourcelist.WithdrawFee" @keyup.native="proving2($event)" min="0">
              <template slot="append">元/笔</template>
            </el-input>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="flex align-center">
            <span class="tit_span nowarp">新增服务商：</span>
            <el-input type="number" v-model="Sourcelist.Facilitator" @keyup.native="proving2($event)" min="0">
              <template slot="append">元/个</template>
            </el-input>
          </div>
        </el-col>

      </el-row>
      <el-button class="float_right" type="primary" @click="SaveSourceFun">保存设置</el-button>
    </div>
    <h4>分红配置活动列表</h4>
    <div class="header_row flex align-center flex-wrap" v-if="hasBtn('Configuration:query')">
      <div class="flex align-center m-r-2">
        <span>榜单名称：</span>
        <el-input v-model="searchObj.RankName" placeholder="请输入内容" clearable></el-input>
      </div>

      <div class="flex align-center m-r-2">
        <span>活动时间：</span>
        <el-date-picker v-model="searchObj.timeValue" type="datetimerange" value-format="yyyy-MM-dd HH:mm:ss" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" clearable>
        </el-date-picker>
      </div>
      <el-button type="primary" @click="(searchObj.currentPage = 1), getListFun()">查询</el-button>
    </div>

    <div class="m-b-1 neck_box flex align-center space-between">
      <div>
        <el-button v-if="hasBtn('Configuration:add')" type="success" @click="showDialog('add')">新增数据</el-button>
        <!-- <el-button type="danger">批量删除</el-button> -->
        <el-button v-if="hasBtn('Configuration:export')" type="warning" disabled>导出列表</el-button>
      </div>
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="searchObj.currentPage" :max-height="tableHeight" :page-size="searchObj.pageSize" :page-sizes="[10, 20, 50, 100, 200]" layout="total,sizes, prev, pager, next, jumper" :total="pageTotal">
      </el-pagination>
    </div>
    <el-table class="m-b-1" :data="tableData" border stripe style="width: 100%" v-loading="tableLoading" sortable="custom" @sort-change="sortChange">
      <el-table-column prop="index" label="序号" align="center">
      </el-table-column>
      <el-table-column prop="RankName" label="榜单名称" align="center">
      </el-table-column>
      <el-table-column width="300" label="上榜条件" align="center">
        <template slot-scope="scope">{{scope.row.Target + '名直属服务商,达销售额' + scope.row.EverySales +'元'}}</template>
      </el-table-column>
      <el-table-column width="100" label="团队总销售额" align="center">
        <template slot-scope="scope">
          <div>{{scope.row.TotalSales + '元'}}</div>
        </template>
      </el-table-column>
      <el-table-column width="100" label="总奖励名额" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.AwardNum == 0">不限名额</div>
          <div v-else>{{scope.row.AwardNum + '个'}}</div>
        </template>
      </el-table-column>
      <el-table-column label="奖励设置" align="center">
        <template slot-scope="scope">{{'前' + scope.row.AwardBefore + '名平分' + scope.row.Bisect +'%'}}</template>
      </el-table-column>
      <!-- <el-table-column label="销售额" align="center">
        <template slot-scope="scope">{{scope.row.Saleroom + '元'}}</template>
      </el-table-column>
      <el-table-column width="100" label="技术服务费" align="center">
        <template slot-scope="scope">{{scope.row.ServiceCharge + '元'}}</template>
      </el-table-column>
      <el-table-column width="100" label="技术服务年费" align="center">
        <template slot-scope="scope">{{scope.row.ServiceChargeYear + '元'}}</template>
      </el-table-column>
      <el-table-column width="100" label="平台管理费" align="center">
        <template slot-scope="scope">{{scope.row.PlatformFee + '元'}}</template>
      </el-table-column>
      <el-table-column width="100" label="提现手续费" align="center">
        <template slot-scope="scope">{{scope.row.WithdrawFee + '元'}}</template>
      </el-table-column> -->

      <el-table-column prop="StartTime" label="活动开始时间" width="180" align="center">
      </el-table-column>
      <el-table-column prop="EndTime" label="活动结束时间" width="180" align="center">
      </el-table-column>
      <el-table-column label="活动规则" align="center" show-overflow-tooltip sortable>
        <template v-slot:default="slotProps">
          <div class="content" v-html="slotProps.row.Remark"></div>
        </template>
      </el-table-column>
      <el-table-column label="是否有效" align="center">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.IsActive" @change="switchChange(scope.row.Id, scope.row.IsActive)" :disabled="!hasBtn('Configuration:active')">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="220" v-if="hasBtn('Configuration:delete') || hasBtn('Configuration:edit')">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)" v-if="hasBtn('Configuration:edit')" type="primary">编辑</el-button>

          <el-button v-if="hasBtn('Configuration:delete')" size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="30%" destroy-on-close>
      <el-form ref="dataForm" :rules="rules" :model="addObj" label-position="right" label-width="140px">
        <el-form-item label="榜单名称" prop="RankName">
          <el-input class="flex-1" v-model="addObj.RankName"></el-input>
        </el-form-item>
        <el-form-item label="活动时间：" prop="timeValue">
          <el-date-picker v-model="addObj.timeValue" type="datetimerange" value-format="yyyy-MM-dd HH:mm:ss" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" clearable style="width: 100%">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动规则：" prop="Remark">
          <el-input type="textarea" class="flex-1" v-model="addObj.Remark" placeholder="请输入内容" show-word-limit></el-input>
        </el-form-item>
        <div class="titlesize m-b-1">上榜条件</div>
        <el-form-item label="直属服务商数量：" prop="Target">
          <div class="flex">
            <div class="flex">
              <el-input class="flex-1 m-r-1" v-model="addObj.Target" type="number" min="0" @keyup.native="proving2($event)"><template slot="append">名</template></el-input>
            </div>
            <div class="flex">
              <span>且每个销售额: </span>
              <el-input class="flex-1 m-l-1" v-model="addObj.EverySales" type="number" min="0" @keyup.native="proving2($event)"><template slot="append">元</template></el-input>
            </div>
          </div>
        </el-form-item>
        <div class="Remarktitle m-b-2 m-l-2">( 注: 例设置3个50万的直属服务商,不管有多少直属服务商,至少需要3个销售额达到50万的 )</div>
        <el-form-item label="团队总销售额：" prop="TotalSales">
          <el-input class="flex-1" v-model="addObj.TotalSales" type="number" min="0" @keyup.native="proving2($event)"><template slot="append">元</template></el-input>
        </el-form-item>
        <div class="flex">
          <div class="titlesize m-b-1 m-r-2">奖励设置</div>
          <div class=" m-t-1 m-l-2 m-b-2">
            <el-radio @click="addObj.AwardNum = 0" v-model="Quotaval" label="1">不限名额</el-radio>
            <el-radio v-model="Quotaval" label="2">独立设置名额</el-radio>
          </div>
        </div>
        <el-form-item label="总奖励名额：" prop="AwardNum">
          <div v-if="Quotaval == '1'">
            <span>不限</span>
          </div>
          <div v-else>
            <el-input class="flex-1" v-model="addObj.AwardNum" min="0" type="number" @keyup.native="proving2($event)"><template slot="append">个</template></el-input>
          </div>
        </el-form-item>
        <el-form-item label="奖金设置：" prop="AwardBefore">
          <div class="flex">
            <div class="flex">
              <span>前</span>
              <el-input class="flex-1 m-l-1 m-r-1" v-model="addObj.AwardBefore" type="number" min="0" @keyup.native="proving2($event)"><template slot="append">名</template></el-input>
            </div>
            <div class="flex">
              <span>平分</span>
              <el-input class="flex-1 m-l-1" v-model="addObj.Bisect" type="number" min="0" @keyup.native="proving2($event)"><template slot="append">%</template></el-input>
            </div>
          </div>
        </el-form-item>
        <div class="Remarktitle m-b-2 m-l-2">( 注: 例前10名先分30%, 剩下的70%再全部分 )</div>

        <!-- <el-form-item label="递减比例：" prop="DecreaseRatio">
          <el-input class="flex-1" v-model="addObj.DecreaseRatio" min="0" @keyup.native="proving2($event, '%')" type="number">
            <template slot="append">%</template>
          </el-input>
        </el-form-item> -->

      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveData">确 定</el-button>
      </span>
    </el-dialog>
    <div class="m-t-2 flex flex-end">

    </div>
  </div>
</template>

<script>

  import { getList, save, valid, deleteApi, GetSource, SaveSource } from '@/api/Dividend/DividendSetting'
  export default {
    //import引入的组件需要注入到对象中才能使用
    name: 'leaderboarddeploy',
    components: {},
    data() {
      //这里存放数据
      return {
        Sourcelist: {
          "Id": "00000000-0000-0000-0000-000000000000"
        },
        Quotaval: '1',
        tableLoading: false,
        dialogVisible: false,
        tableHeight: 0,
        searchObj: {
          TypeName: "",
          currentPage: 1,
          pageSize: 10,
          timeValue: ''
        },
        addObj: {
          "Id": "00000000-0000-0000-0000-000000000000",
          "RankName": "", //榜单名称
          "TotalSales": 0, //总销售额
          "Target": 0, //目标金额/个数
          "AwardNum": 0, //奖励名额
          "EverySales": 0, //每个销售额
          "AwardBefore": 0, //奖励前X名
          "Bisect": 0, //平分Y%
          "timeValue": [],
          "Remark": ""
        },
        SModuleOption: [],
        tableData: [
          { isActive: true }
        ],
        pageTotal: 0,
        tableLoading: false,
        dialogType: 'add',
        rules: {
          RankName: [
            { required: true, message: '必填', trigger: 'blur' },
          ],
          Target: [
            { required: true, message: '必填', trigger: 'blur' },
          ],
          EverySales: [
            { required: true, message: '必填', trigger: 'blur' },
          ],
          TotalSales: [
            { required: true, message: '必填', trigger: 'blur' },
          ],
          AwardNum: [
            { required: true, message: '必填', trigger: 'blur' },
          ],
          AwardBefore: [{ required: true, message: '必填', trigger: 'blur' }],
          Bisect: [{ required: true, message: '必填', trigger: 'blur' }],
          timeValue: [{ required: true, message: '必填', trigger: 'blur' }],
        },
        typeOptions: [],
      };
    },
    //监听属性 类似于data概念
    computed: {
      dialogTitle() {
        return this.dialogType == 'add' ? '新增数据' : '编辑数据'
      }
    },
    //监控data中的数据变化
    watch: {},
    filters: {
      translate(val) {
        if (val == 'Pull') {
          val = '激活榜'
        }
        if (val == 'Promotion') {
          val = '推广榜'
        }
        if (val == 'Sell') {
          val = '销售榜'
        }
        return val
      }
    },
    //方法集合
    methods: {
      async GetSourceFun() {
        const obj = { "Id": "00000000-0000-0000-0000-000000000000" }
        const { Data } = await GetSource(obj)
        this.Sourcelist = Data
      },
      SaveSourceFun() {
        const obj = {
          "Id": this.Sourcelist.Id,
          "Saleroom": this.Sourcelist.Saleroom, //销售额
          "ServiceCharge": this.Sourcelist.ServiceCharge, //技术服务费
          "ServiceChargeYear": this.Sourcelist.ServiceChargeYear, //技术服务年费
          "PlatformFee": this.Sourcelist.PlatformFee, //平台管理费
          "WithdrawFee": this.Sourcelist.WithdrawFee, //提现手续费
          "Facilitator": this.Sourcelist.Facilitator //服务商个数
        }

        for (let cell in obj) {
          if (!obj[cell] && this.$getType(obj[cell]) !== 'Number') {
            obj[cell] = 0
          }
        }
        SaveSource(obj).then(res => {
          if (res.Code == 200) {
            this.$notify({
              title: '保存成功',
              message: 'success',
              type: 'success',
              duration: 2000
            })
            this.GetSourceFun()
          }
        }).catch()

      },
      handleSizeChange(val) {
        this.searchObj.pageSize = val
        this.getListFun()
      },
      handleCurrentChange(val) {
        this.searchObj.currentPage = val
        this.getListFun()
      },
      switchChange(id, IsActive) {
        valid({ Id: id }).then(_ => {
          this.$notify({
            title: '切换成功',
            message: 'success',
            type: 'success',
            duration: 2000
          })
          this.getListFun()
        })
      },
      showDialog(type) {
        if (type == 'add') {
          this.addObj = {
            "Id": "00000000-0000-0000-0000-000000000000",
            "RankName": "", //榜单名称
            "TotalSales": 0, //总销售额
            "Target": 0, //目标金额/个数
            "AwardNum": 0, //奖励名额
            "EverySales": 0, //每个销售额
            "AwardBefore": 0, //奖励前X名
            "Bisect": 0, //平分Y%
            "timeValue": [],
            "Remark": ""
          }
        }
        this.dialogType = type
        this.dialogVisible = true

      },
      saveData() {

        for (let item in this.addObj) {
          if (['Remark'].includes(item)) continue
          if (!this.addObj.item) continue
          if (!this.addObj[item] && this.addObj[item] !== 'Number') {
            this.$notify({
              title: '提示',
              message: '请填写完整信息',
              type: 'warning',
              duration: 2000
            })
            return
          }
        }
        let addObj = this.$deepClone(this.addObj)
        if (this.Quotaval == '1') {
          addObj.AwardNum = 0
        }
        addObj['StartTime'] = addObj.timeValue[0]//开始时间
        addObj['EndTime'] = addObj.timeValue[1]//结束时间
        delete addObj.timeValue
        save(addObj).then(res => {
          this.$notify({
            title: '保存成功',
            message: 'success',
            type: 'success',
            duration: 2000
          })
          this.dialogVisible = false
          this.getListFun()
        }).catch(err => {

        })
      },

      handleEdit(index, row) {
        console.log(row)
        if (row.AwardNum == 0) {
          this.Quotaval = '1'
        } else {
          this.Quotaval = '2'
        }
        this.addObj = {
          "Id": row.Id,
          "RankName": row.RankName, //榜单名称
          "TotalSales": row.TotalSales, //总销售额
          "Target": row.Target, //目标金额/个数
          "AwardNum": row.AwardNum, //奖励金额
          "EverySales": row.EverySales,
          "AwardBefore": row.AwardBefore,
          "Bisect": row.Bisect,
          "timeValue": [row.StartTime, row.EndTime],
          "Remark": row.Remark
        }
        this.showDialog('edit')
      },
      handleDelete(index, row) {
        this.$confirm('确认删除此条数据吗？').then(_ => {
          let obj = {
            Id: row.Id
          }
          deleteApi(obj).then(res => {

            this.$notify({
              title: '删除成功',
              message: 'success',
              type: 'success',
              duration: 2000
            })
            this.tableData.splice(index, 1)
            this.pageTotal -= 1
          })
        })


      },
      sortChange(e) {
        let SortType = false
        if (e.order == "ascending") {
          SortType = true
        }
        this.getListFun(e.prop, SortType)
      },
      getListFun(SortName = 'CreateDate', SortType = true) {
        let currentPage = this.searchObj.currentPage
        this.tableLoading = true
        let searchObj = this.$deepClone(this.searchObj)
        let obj = {
          "RankName": searchObj.RankName,
          "StartTime": '',
          "EndTime": '',
          "IsPaging": true, //是否分页
          "SortName": SortName, //排序栏位
          "SortType": SortType,//排序类型｛True：正序；Flase：反序｝
          'PageIndex': currentPage,
          "PageSize": searchObj.pageSize,
        }
        if (this.searchObj.timeValue) {
          obj.StartTime = this.searchObj.timeValue[0]
          obj.EndTime = this.searchObj.timeValue[1]
        }
        getList(obj).then(res => {
          console.log(res.Data)
          if (!res.Data) {
            this.tableData = []
            this.pageTotal = 0
            this.tableLoading = false
            return
          }
          this.tableData = res.Data.map((item, index) => {
            for (let cell in item) {
              if (!item[cell] && this.$getType(item[cell]) !== 'Number') {
                item[cell] = '-'
              }
            }
            return { ...item, index: (index + 1) + ((currentPage - 1) * 10) }
          })
          this.pageTotal = res.Total
          this.tableLoading = false
        }).catch(_ => {
          this.tableLoading = false
          this.tableData = []
          this.pageTotal = 0
        })
      },

      //获取下拉数据
    },
    //生命周期 - 创建完成（可以访问当前this实例）
    created() {
      this.getListFun()
      this.GetSourceFun()
    },
    //生命周期 - 挂载完成（可以访问DOM元素）
    mounted() {

    },
    beforeCreate() { }, //生命周期 - 创建之前
    beforeMount() { }, //生命周期 - 挂载之前
    beforeUpdate() { }, //生命周期 - 更新之前
    updated() { }, //生命周期 - 更新之后
    beforeDestroy() { }, //生命周期 - 销毁之前
    destroyed() { }, //生命周期 - 销毁完成
    activated() { }, //如果页面有keep-alive缓存功能，这个函数会触发
  }
</script>
//@import url(); 引入公共css类
<style  scoped>
  .Leaderboard .tit_span {
    width: 60px;
  }
  .flex-end {
    justify-content: flex-end;
  }
  .font {
    font-size: 20px;
    font-weight: 500;
  }
  .titlesize {
    font-size: 18px;
    border-left: 4px solid #83beff;
    padding-left: 5px;
    height: 20px;
    margin-top: 5px;
  }
  .headder_setting {
    width: 50%;
    min-width: 800px;
    background: #fff;
    box-sizing: border-box;
    box-shadow: 0 0 5px 3px #ececec;
    padding: 10px;
  }
  .headder_setting .tit_span {
    width: 110px;
    font-size: 0.9rem;
  }
  .Remarktitle {
    font-size: 14px;
    line-height: 20px;
    color: red;
  }
  .content {
    height: 40px;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 1;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
</style>